package com.itheima.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.itheima.canal.config.RabbitMQConfig;
import com.itheima.canal.utils.CanalUtils;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @author ZJ
 */
@CanalEventListener
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * td_ad表被修改
     * @param eventType
     * @param rowData
     */
    @ListenPoint(schema = "changgou_business", table = {"tb_ad"},eventType = CanalEntry.EventType.UPDATE)
    public void adUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.err.println("广告数据发生变化");
        //修改前数据
        Map<String, String> beforeMap = CanalUtils.column2Map(rowData.getBeforeColumnsList());
        //修改后数据
        Map<String, String> afterMap = CanalUtils.column2Map(rowData.getAfterColumnsList());
        //用rabbit发送被修改的id
        if(beforeMap!=null&&afterMap!=null){
            rabbitTemplate.convertSendAndReceive(RabbitMQConfig.GOODS_UP_EXCHANGE,"",afterMap.get("id"));
        }
    }
}
